import { topicApi } from '/api/topic'
import { getUser } from '/store/actions'
Page({
  data: {
    user: {} as DomoUser,
    id: 0,
    list: [] as DomoUser[],
    isLastPage: false,
    keyword: '',
    deleteList: [] as DomoUser[],
    publishList: [] as DomoUser[],
  },
  async onLoad(query: any) {
    const id = +query.id
    this.setData({
      id,
      user: getUser(),
    })
    this.getMember()
  },
  onReachBottom() {
    if (this.data.isLastPage) return
    this.getMember()
  },
  /** 输入绑定 */
  onInputComment(e:WechatMiniprogram.Input) {
    const {
      detail: { value },
    } = e
    this.setData({
      keyword: value,
    })
  },
  /** 搜索用户 */
  search() {
    this.setData({
      isLastPage: false,
      list: [],
    })
    this.getMember()
  },
  async getMember() {
    const {
      data: { id, list, publishList, deleteList },
    } = this
    wx.showNavigationBarLoading()
    const lists = await topicApi.getTopicMember({
      id,
      first_creator: 1,
      limit: 15,
      name: this.data.keyword,
      after: list.length > 0 ? list[list.length - 1].id : 0,
    })
    const res = [...list, ...lists]
    if (publishList.length > 0) {
      res.map((i) => (i.can_pub = publishList.some((j) => j.id == i.id) ? 1 : 0))
    }
    if (deleteList.length > 0) {
      res.map((i) => (i.can_pub = deleteList.some((j) => j.id == i.id) ? 0 : 1))
    }
    this.setData({
      list: res,
      isLastPage: res.length < 15,
    })
    wx.hideNavigationBarLoading()
  },
  /** 点击选中/移除用户 */
  chooseUser(e: WechatMiniprogram.CustomEvent) {
    const {
      currentTarget: {
        dataset: { index },
      },
    } = e
    const {
      data: { list },
    } = this
    let {
      data: { publishList, deleteList },
    } = this
    const user = list[index]
    /** 圈主则返回 */
    if (user.id == this.data.user.id) return
    /** 如果有权限 */
    if (user.can_pub == 1) {
      /** 不在新增列表且移除列表无此用户，则添加到移除列表 */
      !publishList.some((i) => i.id == user.id) &&
        !deleteList.some((i) => i.id == user.id) &&
        deleteList.push(user)
      /** 在新加列表，则在新加列表移除该用户 */
      publishList = publishList.some((i) => i.id == user.id)
        ? publishList.filter((i) => i.id != user.id)
        : publishList
    } else {
      /** 没有权限 */
      /** 不在删除列表且新增列表无此用户，则添加到新增列表 */
      !deleteList.some((i) => i.id == user.id) &&
        !publishList.some((i) => i.id == user.id) &&
        publishList.push(user)
      /** 在删除列表，则在删除列表移除该用户 */
      deleteList = deleteList.some((i) => i.id == user.id)
        ? deleteList.filter((i) => i.id != user.id)
        : deleteList
    }
    this.setData({
      deleteList,
      publishList,
      [`list[${index}].can_pub`]: user.can_pub == 0 ? 1 : 0,
    })
  },
  /** 确认操作 */
  beSure(e: WechatMiniprogram.CustomEvent) {
    const {
      currentTarget: {
        dataset: { pub },
      },
    } = e
    const {
      data: { id, publishList, deleteList },
    } = this
    if ((pub == 0 && deleteList.length == 0) || (pub == 1 && publishList.length == 0)) return
    topicApi
      .setPublish(id, {
        can_pub: pub,
        user_id: pub == 0 ? deleteList.map((i) => i.id) : publishList.map((i) => i.id),
      })
      .then(() => {
        wx.showToast({
          title: '设置成功',
          icon: 'none',
        })
        const pages = getCurrentPages()
        const lastPage = pages[pages.length - 2]
        const editPage = pages[pages.length - 3]
        if (editPage.data.form.publish_permission !== 'specialUser') {
          lastPage.setData({
            selected: 3,
          })
          editPage.setData({
            form: {
              ...editPage.data.form,
              publish_permission: editPage.data.publishPermissions[3].value,
            },
            selected: 3,
          })
          topicApi.updataTopic(id, {
            publish_permission: 'specialUser',
          })
        }
        this.setData({
          publishList: [],
          deleteList: [],
        })
        this.search()
      })
      .catch(() => {
        wx.showToast({
          title: '操作失败',
          icon: 'none',
        })
      })
  },
})
